<template>
  <div>
    <el-card>
      <template #header>
        <div class="flex justify-between items-center">
          <span class="text-lg font-bold">原料幅宽设置</span>
          <div>
            <el-button v-if="!editMode" type="primary" @click="enableEdit">编辑</el-button>
            <el-button v-else type="success" @click="submitForm">保存</el-button>
            <el-button v-if="editMode" @click="resetForm">撤销</el-button>
          </div>
        </div>
      </template>

      <el-form :model="form" label-width="220px" :disabled="!editMode">
        <el-tabs v-model="activeTab">
          <el-tab-pane label="纸板" name="cardboard">
            <el-form-item label="纸板纸长最小值"><el-input-number v-model="form.cardboardLengthMin" /></el-form-item>
            <el-form-item label="纸板纸长最大值"><el-input-number v-model="form.cardboardLengthMax" /></el-form-item>
            <el-form-item label="纸板纸宽单位" prop="cardboardWidthUnit">
              <el-select v-model="form.cardboardWidthUnit" placeholder="请选择纸板纸宽单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="纸板纸长单位" prop="cardboardLengthUnit">
              <el-select v-model="form.cardboardLengthUnit" placeholder="请选择纸板纸长单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="纸板用料纸长小数限定"><el-input-number v-model="form.useMaterialLengthLimit" /></el-form-item> -->
			<el-form-item label="纸板用料纸长小数限定" prop="useMaterialLengthLimit">
			  <el-select v-model="form.useMaterialLengthLimit" placeholder="请选择纸板用料纸长小数限定">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.MATERIAL_DECIMAL_PLACES)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <el-form-item label="纸板纸度可下调值"><el-input-number v-model="form.paperDegreeCuttableValue" /></el-form-item>
            <el-form-item label="纸板纸度是否可下调"><el-switch v-model="form.paperDegreeIsCuttable" /></el-form-item>
            <el-form-item label="纸板纸长可下调值"><el-input-number v-model="form.paperLengthCuttableValue" /></el-form-item>
            <el-form-item label="纸板纸长是否可下调"><el-switch v-model="form.paperLengthIsCuttable" /></el-form-item>
            <el-form-item label="纸板选幅宽以最大损耗值"><el-input-number v-model="form.selectedWidthMaxLoss" /></el-form-item>
            <el-form-item label="纸板纸度大度不限损耗值"><el-input-number v-model="form.paperDegreeUnlimitedLoss" /></el-form-item>
          </el-tab-pane>

          <el-tab-pane label="面纸" name="face">
            <el-form-item label="面纸纸长最小值"><el-input-number v-model="form.facePaperLengthMin" /></el-form-item>
            <el-form-item label="面纸纸长最大值"><el-input-number v-model="form.facePaperLengthMax" /></el-form-item>
            <el-form-item label="面纸纸宽单位" prop="facePaperWidthUnit">
              <el-select v-model="form.facePaperWidthUnit" placeholder="请选择面纸纸宽单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="面纸纸长单位" prop="facePaperLengthUnit">
              <el-select v-model="form.facePaperLengthUnit" placeholder="请选择面纸纸长单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="面纸用料纸长小数限定"><el-input-number v-model="form.useMaterialFaceLengthLimit" /></el-form-item> -->
			<el-form-item label="面纸用料纸长小数限定" prop="useMaterialFaceLengthLimit">
			  <el-select v-model="form.useMaterialFaceLengthLimit" placeholder="请选择面纸用料纸长小数限定">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.MATERIAL_DECIMAL_PLACES)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <el-form-item label="面纸纸度下调值"><el-input-number v-model="form.facePaperDegreeCuttableValue" /></el-form-item>
            <el-form-item label="面纸纸度是否可下调"><el-switch v-model="form.facePaperDegreeIsCuttable" /></el-form-item>
            <el-form-item label="面纸纸长下调值"><el-input-number v-model="form.facePaperLengthCuttableValue" /></el-form-item>
            <el-form-item label="面纸纸长是否可下调"><el-switch v-model="form.facePaperLengthIsCuttable" /></el-form-item>
            <el-form-item label="面纸选幅宽最损耗值"><el-input-number v-model="form.selectedFaceWidthMaxLoss" /></el-form-item>
            <!-- <el-form-item label="面纸算料规则"><el-input v-model="form.facePaperCalculationRule" /></el-form-item> -->
			<el-form-item label="面纸算料规则" prop="facePaperCalculationRule">
			  <el-select v-model="form.facePaperCalculationRule" placeholder="请选择面纸算料规则">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
          </el-tab-pane>

          <el-tab-pane label="坑纸" name="pit">
            <el-form-item label="坑纸纸长最小值"><el-input-number v-model="form.pitPaperLengthMin" /></el-form-item>
            <el-form-item label="坑纸纸长最大值"><el-input-number v-model="form.pitPaperLengthMax" /></el-form-item>
            <el-form-item label="坑纸纸宽单位" prop="pitPaperWidthUnit">
              <el-select v-model="form.pitPaperWidthUnit" placeholder="请选择坑纸纸宽单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="坑纸纸长单位" prop="pitPaperLengthUnit">
              <el-select v-model="form.pitPaperLengthUnit" placeholder="请选择坑纸纸长单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <!-- <el-form-item label="坑纸用料纸长小数限定"><el-input-number v-model="form.useMaterialPitLengthLimit" /></el-form-item> -->
			<el-form-item label="坑纸用料纸长小数限定" prop="useMaterialPitLengthLimit">
			  <el-select v-model="form.useMaterialPitLengthLimit" placeholder="请选择坑纸用料纸长小数限定">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.MATERIAL_DECIMAL_PLACES)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <el-form-item label="坑纸纸度可下调值"><el-input-number v-model="form.pitPaperDegreeCuttableValue" /></el-form-item>
            <el-form-item label="坑纸纸度是否可下调"><el-switch v-model="form.pitPaperDegreeIsCuttable" /></el-form-item>
            <el-form-item label="坑纸纸长可下调值"><el-input-number v-model="form.pitPaperLengthCuttableValue" /></el-form-item>
            <el-form-item label="坑纸纸长是否可下调"><el-switch v-model="form.pitPaperLengthIsCuttable" /></el-form-item>
            <el-form-item label="坑纸选幅宽最大损耗值"><el-input-number v-model="form.selectedPitWidthMaxLoss" /></el-form-item>
            <el-form-item label="坑纸算料规则" prop="pitPaperCalculationRule">
              <el-select v-model="form.pitPaperCalculationRule" placeholder="请选择坑纸算料规则">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.PAPER_CARD_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-tab-pane>

          <el-tab-pane label="其他" name="other">
            <!-- <el-form-item label="选幅宽以">
              <el-input v-model="form.selectedWidthBasis" />
            </el-form-item> -->
			<el-form-item label="选幅宽以" prop="selectedWidthBasis">
			  <el-select v-model="form.selectedWidthBasis" placeholder="选幅宽以">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.SELECTED_WIDTH_BASIS)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <!-- <el-form-item label="核算单单价以">
              <el-input v-model="form.calculationUnitBasis" />
            </el-form-item> -->
			<el-form-item label="核算单单价以" prop="calculationUnitBasis">
			  <el-select v-model="form.calculationUnitBasis" placeholder="核算单单价以">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.CALCULATION_UNIT_BASIS)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <!-- <el-form-item label="纸板用料默认来源">
              <el-input v-model="form.cardboardMaterialDefaultSource" />
            </el-form-item> -->
			<el-form-item label="纸板用料默认来源" prop="cardboardMaterialDefaultSource">
			  <el-select v-model="form.cardboardMaterialDefaultSource" placeholder="纸板用料默认来源">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <!-- <el-form-item label="白板用料默认来源">
              <el-input v-model="form.whiteboardMaterialDefaultSource" />
            </el-form-item> -->
			<el-form-item label="白板用料默认来源" prop="whiteboardMaterialDefaultSource">
			  <el-select v-model="form.whiteboardMaterialDefaultSource" placeholder="白板用料默认来源">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <!-- <el-form-item label="坑纸用料默认来源">
              <el-input v-model="form.pitPaperMaterialDefaultSource" />
            </el-form-item> -->
			<el-form-item label="坑纸用料默认来源" prop="pitPaperMaterialDefaultSource">
			  <el-select v-model="form.pitPaperMaterialDefaultSource" placeholder="坑纸用料默认来源">
			    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.SOURCE)" :key="dict.value" :label="dict.label" :value="dict.value" />
			  </el-select>
			</el-form-item>
            <el-form-item label="刀模尺寸单位" prop="dieCuttingSizeUnit">
              <el-select v-model="form.dieCuttingSizeUnit" placeholder="请选择刀模尺寸单位">
                <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DIE_CUTTING_SIZE_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-tab-pane>
        </el-tabs>

        <!-- <el-form-item label="横纵压小数位表示">
          <el-input v-model="form.horizontalVerticalPressureNumDisplay" />
        </el-form-item> -->
		
		<el-form-item label="横纵压小数位表示" prop="horizontalVerticalPressureNumDisplay">
		  <el-select v-model="form.horizontalVerticalPressureNumDisplay" placeholder="横纵压小数位表示">
		    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.UNIT_PRICE_ROUNDING_MODE)" :key="dict.value" :label="dict.label" :value="dict.value" />
		  </el-select>
		</el-form-item>
        <!-- <el-form-item label="压线单位">
          <el-input v-model="form.pressLineUnit" />
        </el-form-item> -->
		<el-form-item label="压线单位" prop="pressLineUnit">
		  <el-select v-model="form.pressLineUnit" placeholder="压线单位">
		    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
		  </el-select>
		</el-form-item>
        <!-- <el-form-item label="钉条单位">
          <el-input v-model="form.stapleUnit" />
        </el-form-item> -->
		<el-form-item label="钉条单位" prop="stapleUnit">
		  <el-select v-model="form.stapleUnit" placeholder="钉条单位">
		    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
		  </el-select>
		</el-form-item>
        <!-- <el-form-item label="加分单位">
          <el-input v-model="form.addScoreUnit" />
        </el-form-item> -->
		<el-form-item label="加分单位" prop="addScoreUnit">
		  <el-select v-model="form.addScoreUnit" placeholder="加分单位">
		    <el-option v-for="dict in getStrDictOptions(DICT_TYPE.DEFAULT_UNIT)" :key="dict.value" :label="dict.label" :value="dict.value" />
		  </el-select>
		</el-form-item>
        <el-form-item label="用料采购纸度下调提示">
          <el-switch v-model="form.materialPurchasePaperDegreeAdjustmentTip" />
        </el-form-item>
        <el-form-item label="自定义两片接">
          <el-switch v-model="form.customTwoPiece" />
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>


<script setup lang="ts">
	import { ref, reactive, onMounted } from 'vue'
	import { getStrDictOptions, getBoolDictOptions, DICT_TYPE } from '@/utils/dict'
	import { ElMessage, ElMessageBox } from 'element-plus'
	import { BaseOrderPaperSettingApi } from '@/api/erp/baseorderpapersetting'
	import type { BaseOrderPaperSettingVO } from '@/api/erp/baseOrderPaperSetting'

	const editMode = ref(false)
	const activeTab = ref('cardboard')

	const form = reactive<BaseOrderPaperSettingVO>({
		id: '',
		cardboardLengthMin: 0,
		cardboardLengthMax: 0,
		cardboardWidthUnit: '',
		cardboardLengthUnit: '',
		useMaterialLengthLimit: 0,
		paperDegreeCuttableValue: 0,
		paperDegreeIsCuttable: false,
		paperLengthCuttableValue: 0,
		paperLengthIsCuttable: false,
		selectedWidthMaxLoss: 0,
		paperDegreeUnlimitedLoss: 0,

		facePaperLengthMin: 0,
		facePaperLengthMax: 0,
		facePaperWidthUnit: '',
		facePaperLengthUnit: '',
		useMaterialFaceLengthLimit: 0,
		facePaperDegreeCuttableValue: 0,
		facePaperDegreeIsCuttable: false,
		facePaperLengthCuttableValue: 0,
		facePaperLengthIsCuttable: false,
		selectedFaceWidthMaxLoss: 0,
		facePaperCalculationRule: '',

		pitPaperLengthMin: 0,
		pitPaperLengthMax: 0,
		pitPaperWidthUnit: '',
		pitPaperLengthUnit: '',
		useMaterialPitLengthLimit: 0,
		pitPaperDegreeCuttableValue: 0,
		pitPaperDegreeIsCuttable: false,
		pitPaperLengthCuttableValue: 0,
		pitPaperLengthIsCuttable: false,
		selectedPitWidthMaxLoss: 0,
		pitPaperCalculationRule: '',

		selectedWidthBasis: '',
		calculationUnitBasis: '',
		cardboardMaterialDefaultSource: '',
		whiteboardMaterialDefaultSource: '',
		pitPaperMaterialDefaultSource: '',
		dieCuttingSizeUnit: '',
		horizontalVerticalPressureNumDisplay: '',
		pressLineUnit: '',
		stapleUnit: '',
		addScoreUnit: '',
		materialPurchasePaperDegreeAdjustmentTip: false,
		customTwoPiece: false,

		creator: '',
		createTime: '',
		updater: '',
		updateTime: '',
		tenantId: '',
		deleted: false
	})

	const loadData = async () => {
		try {
			const res = await BaseOrderPaperSettingApi.getBaseOrderPaperSetting()
			Object.assign(form, res)
		} catch (err) {
			ElMessage.error('加载配置失败')
		}
	}

	const submitForm = async () => {
		try {
			await ElMessageBox.confirm('确定保存修改吗？', '提示', { type: 'warning' })
			await BaseOrderPaperSettingApi.updateBaseOrderPaperSetting(form)
			ElMessage.success('保存成功')
			editMode.value = false
		} catch (err) {
			ElMessage.error('保存失败')
		}
	}

	const enableEdit = () => {
		editMode.value = true
	}

	const resetForm = () => {
		ElMessageBox.confirm('确定撤销所有修改并恢复原始配置？', '提示', {
			type: 'warning'
		}).then(() => {
			loadData()
			editMode.value = false
		})
	}

	onMounted(() => {

		loadData()
	})
</script>

<style scoped>
	.el-form-item {
		margin-bottom: 16px;
	}
</style>